#!/usr/bin/python2
# -*- coding: UTF-8 -*-

import logging
import urllib2
import urllib
import sys
import os
import re
import json
import codecs
import copy
import ConfigParser
import threading

from base import url_api

class CralingThread(threading.Thread):
    def __init__(self, url, save_dir, count, suffix_list):
        threading.Thread.__init__(self)
        self.url = url
        self.save_dir = save_dir
        self.count = count
        self.suffix_list = suffix_list
        self.start()

    def __del__(self):
        pass

    def run(self):
        logging.info("get url: " + self.url)
        response = urllib2.urlopen(self.url)
        conent = response.read()
        pass
    pass

class JobsService(threading.Thread):
    config_file = "./config/image.json"
    thread_num = 0
    max_count = 0

    def __init__(self):
        pass

    def __del__(self):
        pass

    def startup(self):
        if not self.__readConf(self.config_file):
            logging.error("read config %s failed" % self.config_file)
            return False

        self.start()

        return True

    def run(self):
        root_dir = self.config_dict['root_dir']
        thread_count = self.config_dict['thread_count']
        sites = self.config_dict['sites']
        for site in sites:
            url_pattern = site['url_pattern']
            directoy = site['directoy']
            count = site['count']
            keyword_list = site['keywords']
            suffix_list = site['suffix']

            for keyword in keyword_list:
                url =  url_pattern.replace("%s", keyword)
                craling = CralingThread(url, root_dir + '/' + directoy, count, suffix_list)
                pass
            pass
        pass

    def __readConf(self, config_path):
        if not os.path.exists(config_path):
            logging.error("don't exist config file " + config_path)
            return False

        config_file = open(config_path, "r")
        content = config_file.read()

        try:
            self.config_dict = json.loads(content)
        except Exception, e:
            logging.error("read config file:'%s' failed: %s" % (config_path, e))
            return False

        return True

